package com.wj.algorithm.lintcode.integer;

import com.wj.algorithm.ArrayUtil;

public class P172 {
    public int removeElement(int[] a, int elem) {
        if(a == null) {
            return 0;
        }
        int len = a.length;
        if(len < 1) {
            return 0;
        }
        
        int[] temp = new int[len];
        int deletedCount = 0;
        for(int i = 0; i < len; i ++) {
            if(a[i] == elem) {
                temp[i] = -1;
                deletedCount ++;
            }
        }
        
        int rsIndex = 0;
        for(int i = 0; i < len; i ++) {
            if(temp[i] < 0) {
                continue;
            }
            a[rsIndex] = a[i];
            rsIndex ++;
        }
        return len - deletedCount;
    }

    public static void main(String[] args) {
        int[] a = new int[] { 0, 4, 4, 0, 0, 2, 4, 4 };
        int v = new P172().removeElement(a, 4);
        System.out.println(ArrayUtil.toString(a));
        System.out.println(v);
    }
}
